package cn.liulin.java8.simplestudy;

import com.alibaba.fastjson.JSON;

import java.util.Iterator;

/**
 * cn.liulin.java8.simplestudy$
 *
 * @author ll
 * @date 2024-03-22 15:47:15
 **/
public class MyLinkedList<T> {
    // 默认的数据量
    public int size = 0;
    public Node<T> firstNode;

    public MyLinkedList() {
        firstNode = new Node<>();
    }

    public class Node<T> {
        private T t;
        private Node<T> pre;
        private Node<T> next;

        public T getT() {
            return t;
        }

        public void setT(T t) {
            this.t = t;
        }

        public Node<T> getPre() {
            return pre;
        }

        public void setPre(Node<T> pre) {
            this.pre = pre;
        }

        public Node<T> getNext() {
            return next;
        }

        public void setNext(Node<T> next) {
            this.next = next;
        }
    }

    public boolean add(T t) {
        if (size == 0) {
            firstNode.t = t;
        } else {
            Node<T> currentNode = firstNode;
            while (currentNode.next != null) {
                currentNode = currentNode.next;
            }
            currentNode.next = new Node<>();
            currentNode.next.t = t;
            currentNode.next.pre = currentNode;
        }
        size++;
        return true;
    }

    public T get(int index) {
        if (index < 0 || index >= size) {
            throw new RuntimeException("越界异常");
        }
        int tmp = 0;
        Node<T> currentNode = firstNode;
        while (tmp < index) {
            currentNode = currentNode.next;
            tmp++;
        }
        return currentNode.t;
    }

    public T remove(int index) {
        if (index < 0 || index >= size) {
            throw new RuntimeException("越界异常");
        }
        int tmp = 0;
        Node<T> currentNode = firstNode;
        while (tmp < index) {
            currentNode = currentNode.next;
            tmp++;
        }
        if (currentNode != firstNode) {
            Node<T> preNode = currentNode.pre;
            Node<T> nextNode = currentNode.next;
            if (nextNode != null) {
                nextNode.pre = preNode;
            }
            preNode.next = nextNode;
        } else {
            if (currentNode.next == null) {
                currentNode.t = null;
            } else {
                firstNode = currentNode.next;
                currentNode.pre = null;
            }
        }
        size--;
        return currentNode.t;
    }

    @Override
    public String toString() {
        if (size == 0) {
            return "[]";
        } else {
            MyLinkedListItr itr = itr();
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            while (itr.hasNext()) {
                T next = itr.next();
                sb.append(next);
                if (itr.hasNext()) {
                    sb.append(", ");
                }
            }
            sb.append("]");
            return sb.toString();
        }
    }

    public MyLinkedListItr itr() {
        return new MyLinkedListItr();
    }

    public class MyLinkedListItr implements Iterator<T> {

        private int index;
        private Node<T> currentNode = firstNode;

        @Override
        public boolean hasNext() {
            return index < size;
        }

        @Override
        public T next() {
            if (currentNode == null) {
                throw new RuntimeException("越界异常");
            }
            T t = currentNode.t;
            currentNode = currentNode.next;
            index++;
            return t;
        }
    }

    public static void main(String[] args) {
        MyLinkedList<Integer> myLinkedList = new MyLinkedList<>();
        for (int i = 0; i < 100; i++) {
            myLinkedList.add(i);
        }
        System.out.println(myLinkedList.toString());

    }
}
